package peer.rdp.message;

import java.net.DatagramPacket;
import java.util.ArrayList;

import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;
import peer.tools.MessageType;


public class ALMessage extends Message{
	ArrayList<Integer> ackList;
	byte[] dpData=null;
	@SuppressWarnings("unchecked")
	int lastRead;
	@SuppressWarnings("unchecked")
	public ALMessage(ArrayList ackList,int lastRead){
		this.ackList=ackList;
		this.lastRead=lastRead;
		dpData=new byte[4+4+1+4*ackList.size()];
		sType=MessageType.sType_ALMessage;
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		ByteIntSwitch.toByteArray(lastRead, dpData, 4);
		dpData[8]=(byte) ackList.size();
		
		
		for(int i=0;i<ackList.size();i++){
			int sequence=(Integer)ackList.get(i);
			ByteIntSwitch.toByteArray(sequence, dpData, 9+4*i);  //add: sequence
		}
		dp=new DatagramPacket(dpData,dpData.length);
	}
	
	public ArrayList getHm(){
		return ackList;
	}
	
	public ALMessage(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		lastRead=ByteIntSwitch.toInt(dpData, 4);
		int sum=dpData[8];
		ackList=new ArrayList<Integer>();
		for(int i=0;i<sum;i++){
			int sequence=ByteIntSwitch.toInt(dpData, 9+4*i);
			ackList.add(sequence);
		}
	}
	
	public int getLastRead(){
		return lastRead;
	}
}
